/* eslint no-unused-vars:0 */
import React, { Component } from 'react';
// import PubSub from 'pubsub-js';

import { Icon, Form, Input, Modal, Button } from 'antd';

const FormItem = Form.Item;

class AddForm extends Component {
    state = {
        data: {
            id: '',
            name: ''
        },
        visible: false
    };

    login = (form) => {};

    componentDidMount() {
        // this.pubsub_token = PubSub.subscribe('loginModal', function (topic, message) {
        // 	this.setState({
        // 		visible: message
        // 	})
        // }.bind(this));
    }

    componentWillReceiveProps(nextProps) {
        this.setState({
            data: nextProps.data,
            visible: nextProps.visible
        });
    }

    handleOk = (e) => {
        this.props.form.validateFields((err, values) => {
            if (!err) {
                if (this.state.data.id == '') this.handleAdd(values);
                else this.handleUpdate(values);

                this.props.handleForm();
            }
        });
    };

    handleAdd = (data) => {
        this.$post('/publicSentiment/save', data)
            .then((res) => {
                if (res) {
                    this.props.getReturn(res);
                    this.$showNotification('success', '添加成功');
                } else this.$showNotification('error', '添加失败');
            })
            .catch((e) => {
                console.log(e);
            });
    };

    handleUpdate = (data) => {
        data['id'] = this.state.data.id;
        this.$post('/publicSentiment/update', data)
            .then((res) => {
                if (res) {
                    this.props.getReturn(res);
                    this.$showNotification('success', '修改成功');
                } else this.$showNotification('error', '修改失败');
            })
            .catch((e) => {
                console.log(e);
            });
    };

    handleCancel = (index) => {
        this.props.handleForm();
    };

    render() {
        const { getFieldDecorator } = this.props.form;

        return (
            <Modal
                title="添加或修改"
                visible={this.state.visible}
                okText="提交"
                cancelText="取消"
                onOk={this.handleOk}
                onCancel={this.handleCancel}>
                <Form>
                    <FormItem>
                        {getFieldDecorator('name', {
                            rules: [
                                {
                                    required: true,
                                    message: '请输入舆情主题的名称'
                                }
                            ],
                            initialValue: this.state.data.name
                        })(
                            <Input
                                prefix={
                                    <Icon
                                        type="user"
                                        style={{ color: 'rgba(0,0,0,.25)' }}
                                    />
                                }
                                placeholder="舆情主题"
                            />
                        )}
                    </FormItem>
                </Form>
            </Modal>
        );
    }
}

const AddModal = Form.create()(AddForm);

export default AddModal;
